<template>
  <div class="newsmanageIndex">
    <button class="bbutton" @click="searchnews">查询</button>
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, pageSize * currentPage)
      "
      border
      style="width: 100%"
      height="850"
    >
      <el-table-column prop="news_id" label="id" width="50"> </el-table-column>
      <el-table-column prop="title" label="标题" width="200"> </el-table-column>
      <el-table-column prop="source" label="新闻来源" width="80">
      </el-table-column>
      <el-table-column prop="url" label="url"> </el-table-column>
      <el-table-column prop="publish_time" label="发行时间"> </el-table-column>
      <el-table-column prop="category" label="种类" width="80">
      </el-table-column>
      <el-table-column
        prop="content"
        label="内容"
        width="110"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column prop="keywords" label="关键字"> </el-table-column>
      <el-table-column prop="sentiment" label="情感分类" width="100">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            style="float: left"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            style="float: right"
            @click="handleScan(scope.$index, scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: center; margin-top: 30px">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        @current-change="current_change"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        id: "",
        title: "",
        source: "",
        url: "",
        publish_time: "",
        category: "",
        content: "",
        keywords: "",
        sentiment: "",
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
    };
  },
  created: function () {
    this.showlist();
    //console.log("tabledata",this.tableData)
  },
  methods: {
    showlist() {
      const self = this;
      self
        .$axios({
          method: "post",
          url: "/api/news/show",
        })
        .then((resp) => {
          // console.log(resp)
          var tmptable = resp.data.data;
          for (let i = 0; i < tmptable.length - 1; i++) {
            var tmp = tmptable[i];
            var target = Math.random() * (tmptable.length - i) + i;
            target = parseInt(target);
            tmptable[i] = tmptable[target];
            tmptable[target] = tmp;
          }
          this.tableData = tmptable;
          // console.log('total:',this.total)
          this.total = this.tableData.length;
        });
    },
    handleEdit(index, row) {
      // console.log(index, row);
      const self = this;
      self
        .$axios({
          method: "post",
          url: "/api/news/edit",
          data: {
            news_id: row.news_id,
            name: "title",
            cont: row.title,
          },
          headers: {
            Authorization: window.sessionStorage.getItem("token"),
          },
        })
        .then((res) => {
          // console.log("返回的东西：",res)
          if (res.data.statusCode == 0) {
            alert(res.data.message);
          } else {
            //alert(res.data.message)
            this.$router.push({
              name: "NewsEdit",
              query: {
                id: row.news_id,
                title: row.title,
                source: row.source,
                url: row.url,
                publish_time: row.publish_time,
                category: row.category,
                content: row.content,
                keywords: row.keywords,
                sentiment: row.sentiment,
              },
            });
          }
        })
        .catch((e) => {
          alert(e.response.data.message);
        });

      // window.location.replace('mainlayout/NewsEdit')
      // window.location.replace('NewsEdit')
    },
    handleScan(index, row) {
      // console.log(index, row);
      const self = this;
      this.$router.push({
        name: "NewsScan",
        query: {
          id: row.news_id,
          title: row.title,
          source: row.source,
          url: row.url,
          publish_time: row.publish_time,
          category: row.category,
          content: row.content,
          keywords: row.keywords,
          sentiment: row.sentiment,
        },
      });
    },
    current_change: function (currentPage) {
      this.currentPage = currentPage;
    },
    searchnews() {
      console.log("search news!");
      // window.location.replace("NewsSearch");
      this.$router.push({
        name: "NewsSearch",
      });
    },
  },
};
</script>

<style scoped="scoped">
</style>
